package cn.xc.org.service.impl;

import cn.xc.basic.service.impl.BaseServiceImpl;
import cn.xc.org.domain.Department;
import cn.xc.org.mapper.DepartmentMapper;
import cn.xc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> deptTree() {
        List<Department> departments = departmentMapper.loadAll();
        HashMap<Long, Department> map = new HashMap<>();
        for (Department department : departments){
            map.put(department.getId(),department);
        }
        ArrayList<Department> deptTree = new ArrayList<>();
        for (Department department : departments){
            if (department.getParent_id()==null){
                deptTree.add(department);
            }else {
                Long parentId = department.getParent_id();
                Department detpDepartment = map.get(parentId);
                detpDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
